import { ref } from 'vue'
import useDictStore from '@/store/modules/dict'
import { getDicts } from '@/api/system/dict/data'

export function useDict(...args) {
  const res = ref({});
  
  return new Promise((resolve) => {
    const promises = args.map((dictType) => {
      res.value[dictType] = [];
      const dicts = useDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
        return Promise.resolve(dicts);
      } else {
        return getDicts(dictType).then(resp => {
          const dictData = resp.data.map(p => ({ 
            label: p.dictLabel, 
            value: p.dictValue, 
            elTagType: p.listClass, 
            elTagClass: p.cssClass 
          }));
          res.value[dictType] = dictData;
          useDictStore().setDict(dictType, dictData);
          return dictData;
        });
      }
    });

    Promise.all(promises).then(() => {
      resolve(res);
    });
  });
} 